﻿using KPIS.GERP.GAMS.APP.App_UI.Commons;
using KPIS.GERP.GAMS.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ReportRQPQDN : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_BGM_MAS_Source source = new D_BGM_MAS_Source();
        D_ACM_MiscReport rpt = new D_ACM_MiscReport();
        MailMerge mailMerge = new MailMerge();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rblType_SelectedIndexChanged(null, null);
                LoadDDLBgmSourceSeq();
            }
        }

        protected void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = source.GetList();
            ddlBgmSourceSeq.DataBind();

            main.InsertEmptyDDLAll(ddlBgmSourceSeq);
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            string strReportHeader = "";
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            DateTime? dtStartDate = txtDateStart.GetDateValue();
            DateTime? dtEndDate = txtDateEnd.GetDateValue();

            if (rblType.SelectedValue == "1" && (!dtStartDate.HasValue || !dtEndDate.HasValue))
            {
                lblWarning.Visible = true;
                return;
            }

            if (rblType.SelectedValue == "2" && (string.IsNullOrEmpty(txtRefNoStart.Text) || string.IsNullOrEmpty(txtRefNoEnd.Text)))
            {
                lblWarning.Visible = true;
                return;
            }

            DataTable dtb = rpt.ReportRQ_PQ_DN(strBgmSourceSeq, txtRefNoStart.Text, txtRefNoEnd.Text, dtStartDate, dtEndDate);
            Dictionary<string, DataTable> dic = new Dictionary<string, DataTable>();
            dic.Add("A5", dtb);

            Dictionary<string, string> dic2 = new Dictionary<string, string>();

            if (rblType.SelectedValue == "1")
            {
                strReportHeader = string.Format("ตั้งแต่ RQ วันที่ {0} ถึงวันที่ {1}", dtStartDate.Value.ToLongDateString(), dtEndDate.Value.ToLongDateString());
            }
            else
            {
                strReportHeader = string.Format("ตั้งแต่ RQ เลขที่ {0} ถึงเลขที่ {1}", txtRefNoStart.Text, txtRefNoEnd.Text);
            }
            dic2.Add("A3", strReportHeader);

            mailMerge.MergeExcel(Response, Server.MapPath("~/Reports/Templates/RQ-PQ-DN.xlsx"), string.Format("RQ-PQ-DN-{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")), dic, dic2);
        }

        protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblType.SelectedValue == "1")
            {
                PanelDate.Visible = true;
                PanelRefNo.Visible = false;
            }
            else if (rblType.SelectedValue == "2")
            {
                PanelDate.Visible = false;
                PanelRefNo.Visible = true;
            }
        }
    }
}